package com.oss.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.google.gson.Gson;
import com.oss.model.Site;
import com.oss.model.Link;
import com.oss.model.Project;
import com.oss.model.Topic;
import com.oss.validation.BlogValidator;
import com.oss.validation.LinkValidator;
import com.oss.validation.ProjectValidator;

/**
 * @author xueqiang.mi
 * @since 2010-8-12
 */
@Controller
public class AjaxController extends BaseController {

	@RequestMapping(value = "/ajax/admin/deleteLink.do", method = RequestMethod.POST)
	public String ajaxDeleteLink(@RequestParam("id") Long id) {
		lnkService.delete(id);
		return "admin/result";
	}

	@RequestMapping(value = "/ajax/admin/deleteTopic.do", method = RequestMethod.POST)
	public String ajaxDeleteTopic(@RequestParam("id") String id) {
		topicService.delete(Topic.class, id);
		return "admin/result";
	}

	@RequestMapping(value = "/ajax/admin/share.do", method = RequestMethod.POST)
	public String share(@RequestParam("type") String type,
			@RequestParam("obj") String obj) {
		Gson gson = new Gson();
		if (type.equals("Project")) {
			Project prj = gson.fromJson(obj, Project.class);
			if (ProjectValidator.validate(prj)) {
				prjService.add(prj);
			} else {
				return "admin/error";
			}
		} else if (type.equals("Link")) {
			Link lnk = gson.fromJson(obj, Link.class);
			if (LinkValidator.validate(lnk)) {
				lnkService.add(lnk);
			} else {
				return "admin/error";
			}
		} else if (type.equals("Blog")) {
			Site blog = gson.fromJson(obj, Site.class);
			if (BlogValidator.validate(blog)) {
				siteService.add(blog);
			} else {
				return "admin/error";
			}
		}
		return "admin/result";
	}

	@RequestMapping(value = "/ajax/admin/updateLink.do", method = RequestMethod.POST)
	public String ajaxUpdateLink(
			@RequestParam(value = "lnk", required = true) String str_lnk) {
		String[] params = str_lnk.split("-,-");
		Link lnk = lnkService.get(Long.valueOf(params[0].trim()));
		if (lnk != null) {
			lnk.setName(params[1]);
			lnk.setSource(params[2]);
			lnk.setTag(params[3]);
			lnk.setTopic(params[4]);
			lnk.setPrj(params[5]);
			if (params[6].equals("true")) {
				lnk.setEnabled(true);
			} else {
				lnk.setEnabled(false);
			}
		}
		lnkService.save(lnk);
		return "admin/result";
	}

	@RequestMapping(value = "/ajax/admin/updatePrjDesc.do", method = RequestMethod.POST)
	public String ajaxUpdatePrjDesc(
			@RequestParam(value = "prjId") String prjId,
			@RequestParam(value = "prjDesc") String prjDesc) {
		Project prj = prjService.get(prjId);
		if (prj != null) {
			prj.setDescription(prjDesc);
		}
		prjService.save(prj);
		return "admin/result";
	}

	@RequestMapping(value = "/ajax/admin/updateProject.do", method = RequestMethod.POST)
	public String ajaxUpdateProject(
			@RequestParam(value = "prj", required = true) String str_prj) {
		String[] params = str_prj.split("-,-");
		Project prj = prjService.get(params[0]);
		if (prj != null) {
			prj.setName(params[1]);
			prj.setSource(params[2]);
			prj.setLicense(params[3]);
			prj.setLabel(params[4]);
			prj.setTag(params[5]);
			if (params[6].equals("true")) {
				prj.setEnabled(true);
			} else {
				prj.setEnabled(false);
			}
		}
		prjService.save(prj);
		return "admin/result";
	}

	@RequestMapping(value = "/ajax/admin/updateTopic.do", method = RequestMethod.POST)
	public String ajaxUpdateTopic(
			@RequestParam(value = "topic", required = true) String str_topic) {
		String[] params = str_topic.split("-,-");
		if (null == params[0] || "".equals(params[0])) {
			Topic topic = new Topic(params[1]);
			topicService.add(topic);
		} else {
			Topic topic = topicService.get(params[0]);
			if (topic != null) {
				topic.setName(params[1]);
				topic.setAuthor(params[2]);
				topic.setLinkQuantity(Long.valueOf(params[3].trim()));
				if (params[4].equals("true")) {
					topic.setEnabled(true);
				} else {
					topic.setEnabled(false);
				}
			}
			topicService.save(topic);
		}
		return "admin/result";
	}

}
